$('#addType').combobox({
	    onChange:function(v,oldValue){
	    	if(v=='1' || v=='2'){
				$('#chooseABCD').css('display','block');
				// 还原textbox的验证
			    $('#addAnswerA').textbox({required:true});
			    $('#addAnswerB').textbox({required:true});
			    $('#addAnswerC').textbox({required:true});
			    $('#addAnswerD').textbox({required:true});
			    
			}else if(v=='3'){
				// 隐藏答案的四个选项框
				$('#chooseABCD').css('display','none');
                //  清空textbox的值
				$("#addAnswerA").textbox("setValue","");
				$("#addAnswerB").textbox("setValue","");
				$("#addAnswerC").textbox("setValue","");
				$("#addAnswerD").textbox("setValue","");
				//  移除textbox验证
				$('#addAnswerA').textbox({required:false});
				$('#addAnswerB').textbox({required:false});
				$('#addAnswerC').textbox({required:false});
				$('#addAnswerD').textbox({required:false});
			}
	    }
	});
$('#questionTypeUpdate').combobox({
    	 onChange:function(n,oldValue){
 	    	if(n=='1' || n=='2'){
 				$('#chooseabcd').css('display','block');
 				// 还原textbox的验证
 			    $('#updateAnswerA').textbox({required:true});
 			    $('#updateAnswerB').textbox({required:true});
 			    $('#updateAnswerC').textbox({required:true});
 			    $('#updateAnswerD').textbox({required:true});
 			    
 			}else if(n=='3'){
 				// 隐藏答案的四个选项框
 				$('#chooseabcd').css('display','none');
                 //  清空textbox的值
 				$("#updateAnswerA").textbox("setValue","");
 				$("#updateAnswerB").textbox("setValue","");
 				$("#updateAnswerC").textbox("setValue","");
 				$("#updateAnswerD").textbox("setValue","");
 				//  移除textbox验证
 				$('#updateAnswerA').textbox({required:false});
 				$('#updateAnswerB').textbox({required:false});
 				$('#updateAnswerC').textbox({required:false});
 				$('#updateAnswerD').textbox({required:false});
 			}
 	    }
    });

$('#questionTypeDetail').combobox({
	 onChange:function(n,oldValue){
    	if(n=='1' || n=='2'){
			$('#chooseAbcd').css('display','block');
			// 还原textbox的验证
		    $('#detailAnswerA').textbox({required:true});
		    $('#detailAnswerB').textbox({required:true});
		    $('#detailAnswerC').textbox({required:true});
		    $('#detailAnswerD').textbox({required:true});
		    
		}else if(n=='3'){
			// 隐藏答案的四个选项框
			$('#chooseAbcd').css('display','none');
            //  清空textbox的值
			$("#detailAnswerA").textbox("setValue","");
			$("#detailAnswerB").textbox("setValue","");
			$("#detailAnswerC").textbox("setValue","");
			$("#detailAnswerD").textbox("setValue","");
			//  移除textbox验证
			$('#updateAnswerA').textbox({required:false});
			$('#updateAnswerB').textbox({required:false});
			$('#updateAnswerC').textbox({required:false});
			$('#updateAnswerD').textbox({required:false});
		}
    }
});
var question = {
		init : function() {
			question.questionGrid = $('#questionGrid');
			question.addCourse = $('#addCourse');
			question.questionAddDialog = $('#questionAddDialog');
			question.questionCourseUpdate = $('#questionCourseUpdate')
			question.questionAddForm = $('#questionAddForm');
			question.questionUpdateForm = $('#questionUpdateForm');
			question.questionUpdateDialog = $('#questionUpdateDialog');
			question.name = $('#updateName');
			question.questionName = $('#questionName');
			question.questionType = $('#questionType');
			question.questionCourse = $('#questionCourse');
			question.questionDifficulty = $('#questionDifficulty');
			question.questionState = $('#questionState');
			question.questionDetailForm = $('#questionDetailForm');
			question.questionDetailDialog = $('#questionDetailDialog');
			//console.info(question.questionGrid);
			question.questionGrid.datagrid({
			    url: window.ctx +'/QuestionQesServlet?command=listQuestion',
			    fitColumns: true,
			    striped: true,
			    rownumbers: true,
			    pagination: true,
			    pageSize: 10,
			    pageList: [10, 15, 20, 25, 30],
			    pageNumber: 1,
			   /*  sortName: 'age', */
			    sortOrder: 'desc',
			    toolbar: '#questionToolbar',
			    columns:[[
					{field:'id', width:100, checkbox: true},
					{field:'question', title:'题目', width:100, align:'center'},
					{field:'questionTypeStr', title:'题型', width:100, align:'center'},
					{field:'course_name', title:'所属课程', width:100, align:'center'},
					{field:'difficultyStr', title:'难度', width:100, align:'center'},
					{field:'stateStr', title:'状态', width:100, align:'center'},
					{field:'operate', title: '操作', formatter: question.rows.detailButton, width:100, align:'center'},
					// {field:'memo', title:'备注', width:100, align:'center'}
			    ]],
				onDblClickRow: function(rowIndex, rowData) {
					question.toolbar.showUpdateDialog(rowData.id);
				},
				onBeforeLoad: function (param) {
					var question=$('#question').val();
					param.question=question;
			    },
			});
		},
		rows:{
			detailButton:function(value,row,index){
				var myId= row.id;
			return '<a href="#" onclick="question.rows.showDetail('+myId + ','+ index + ')" style="text-decoration:none;">查看详情</a>';	
			},
			showDetail:function(myId){
			
				$.post(window.ctx +'/QuestionQesServlet?command=getQuestion&id='+myId,function(data) {
					if (data) {
						question.questionDetailForm.form('load', {
							/*id: data.id,
							question:data.question,
							attachment:data.attachment,
							question_type:data.questionType,
							course_id:data.courseId,
							//course_id:data.course_id,
							answer_a:data.answerA,
							answer_b:data.answerB,
							answer_c:data.answerC,
							answer_d:data.answerD,
							answer:data.answer,
							difficulty:data.difficulty,
							analysis:data.analysis,
							keywords:data.keywords,
							operateTime:data.operateTime,
							role_id: data.roleId,
							state:data.state,
							memo:data.memo*/
							id: data.id,
							detailName: data.question,
							detailAttachment: data.attachment,
							questionTypeDetail: data.questionType,
							questionCourseDetail:data.courseId,
							detailAnswerA: data.answerA,
							detailAnswerB: data.answerB,
							detailAnswerC: data.answerC,
							detailAnswerD: data.answerD,
							detailAnswer: data.answer,
							questionDifficultyDetail: data.difficulty,
							detailAnalysis: data.analysis,
							detailKeywords: data.keywords
						});
						question.questionDetailDialog.dialog('open');
				}else {
					$.messager.alert('系统提示', '获取信息失败！', 'error')
				     }
				}, 'json');
			},
			detailCloseDialog:function(){
				question.questionDetailDialog.dialog('close');
			}
		},

		toolbar: {
			showAddDialog: function() {
				
				question.addCourse.combotree({
					url: window.ctx +'/ClazzServlet?command=listMenu',
					lines: true,
					multiple: true,
					checkbox: true,
					onlyLeafCheck: true,
					onLoadSuccess: function(node, data) {
						$(this).tree('expandAll')
						$(this).find('span.tree-checkbox').unbind().click(function () {
							var treeObj=question.addCourse.combotree('tree');
							treeObj.tree('select', $(this).parent());
		                    return false;
		                });
					},
					onSelect: function (node) {
						var treeObj=question.addCourse.combotree('tree');
		                var cknodes = treeObj.tree("getChecked");
		                for (var i = 0; i < cknodes.length; i++) {
		                    if (cknodes[i].id != node.id) {
		                    	treeObj.tree("uncheck", cknodes[i].target);
		                    }
		                }
		                if (node.checked) {
		                	treeObj.tree('uncheck', node.target);

		                } else {
		                	treeObj.tree('check', node.target);

		                }

		            }
				}); 
				// question.toolbar.showCombotree();
				question.questionAddDialog.dialog('open');
				question.name.focus();
				// 坑，只能使用id选择器
				/* question.age.numberspinner('setValue', 20); */
			},
			hiddenAddDialog: function() {
				question.questionAddDialog.dialog('close');
				question.toolbar.resetAddForm();
			},
			resetAddForm: function() {
				question.questionAddForm.form('reset');
			},
			add: function() {
				if ($('#questionAddForm').form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					 console.info(question.questionAddForm.serialize());
					$.post(window.ctx+'/QuestionQesServlet?command=addQuestion', $('#questionAddForm').serialize(), function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '新增成功！',
								timeout: 3000,
								showType: 'slide'
							});
							question.toolbar.resetAddForm();
							question.questionGrid.datagrid('reload');
							
						} else {
							$.messager.alert('系统提示', '新增失败！', 'error')
						}
						$.messager.progress('close');
						question.toolbar.hiddenAddDialog();
					});
				}
			},
			showUpdateDialog: function(id) {
				var getData = function(v) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx +'/QuestionQesServlet?command=getQuestion', {id : v}, function(data) {
						if (data) {
							question.questionUpdateForm.form('load', {
								id: data.id,
								updateName: data.question,
								updateAttachment: data.attachment,
								questionTypeUpdate: data.questionType,
								questionCourseUpdate:data.courseId,
								updateAnswerA: data.answerA,
								updateAnswerB: data.answerB,
								updateAnswerC: data.answerC,
								updateAnswerD: data.answerD,
								updateAnswer: data.answer,
								questionDifficultyUpdate: data.difficulty,
								updateAnalysis: data.analysis,
								updateKeywords: data.keywords
							});
							question.questionUpdateDialog.dialog('open');
						} else {
							$.messager.alert('系统提示', '获取信息失败！', 'error')
						}
						$.messager.progress('close');
					}, 'json');
				}
				if (id) {
					getData(id);
				} else {
					var row = question.questionGrid.datagrid('getSelections');
					if (row.length == 1) {
						getData(row[0].id);
					} else {
						$.messager.show({
							title: '系统提示',
							msg: '请选择一行数据进行修改！',
							timeout: 3000,
							showType: 'slide'
						});	
					}
				}
			},
			hiddenUpdateDialog: function() {
				question.questionUpdateDialog.dialog('close');
				question.toolbar.resetUpdateForm();
			},
			hiddenDetailDialog: function() {
				question.questionDetailDialog.dialog('close');
				question.toolbar.resetDetailForm();
			},
			resetUpdateForm: function() {
				question.questionUpdateForm.form('reset');
			},
			update: function() {
				if (question.questionUpdateForm.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx +'/QuestionQesServlet?command=updateQuestion', question.questionUpdateForm.serialize(), function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '修改成功！',
								timeout: 3000,
								showType: 'slide'
							});
							question.toolbar.resetUpdateForm();
							question.questionGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '修改失败！', 'error');
						}
						$.messager.progress('close');
						question.toolbar.hiddenUpdateDialog();
					});
				}
			},
			remove: function() {
				var row = question.questionGrid.datagrid('getSelections');
				if (row.length > 0) {
					var ids = [];
					$.each(row, function(i, v) {
						ids.push(v.id);
					});
					$.post(window.ctx +'/QuestionQesServlet?command=deleteQuestion', {ids: ids.join(',')}, function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '删除成功！',
								timeout: 3000,
								showType: 'slide'
							});
							question.questionGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '删除失败！', 'error')
						}
						$.messager.progress('close');
					});
				} else {
					$.messager.show({
						title: '系统提示',
						msg: '请选择数据进行删除！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
			},
			redo: function() {
				question.questionGrid.datagrid('unselectAll');
			},
			updateState: function() {
				var row = question.questionGrid.datagrid('getSelections');
				if (row.length == 1) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx +'/QuestionQesServlet?command=changeQuestion', {id : row[0].id} , function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '修改成功！',
								timeout: 3000,
								showType: 'slide'
							});
							question.questionGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '修改失败！', 'error');
						}
						$.messager.progress('close');
					});
				} else {
					$.messager.show({
						title: '系统提示',	
						msg: '请选择一行数据进行修改！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
		},
		search:function(){
			question.questionGrid.datagrid('load',{
				questionName: question.questionName.val(),
				questionType: question.questionType.combobox("getValue"),
				questionCourse: question.questionCourse.combobox("getValue"),
				difficulty: question.questionDifficulty.combobox("getValue"),
				state: question.questionState.combobox("getValue")
			});
			question.questionGrid.datagrid('reload');
		}
		},
		
	}

	
	$(function(){
		question.init();
	});